package mysql

import (
	"gorm.io/gorm"
	"rent-ease/rentrpc/rentrpc"
)

type House struct {
	gorm.Model
	Title string  `gorm:"type:varchar(30);not null;commit:标题"`
	Desc  string  `gorm:"type:varchar(30);not null;commit:简介"`
	Price float64 `gorm:"type:decimal(10,2);not null;commit:价格"`
}

func (h *House) HouseAdd(db *gorm.DB) error {
	return db.Debug().Create(h).Error
}

func (h *House) FindHouseById(db *gorm.DB, id int64) error {
	return db.Debug().Where("id = ?", id).First(h).Error
}

func HouseList(db *gorm.DB) ([]*rentrpc.HouseList, error) {
	var list []*rentrpc.HouseList
	err2 := db.Debug().Model(&House{}).
		Select("houses.id as house_id",
			"title",
			"desc",
			"price").Find(&list).Error
	return list, err2
}
